"""全局常量，永久不可变"""

import os
from src.core import log
from typing import Iterator
from src.utils.deep import get_realip
from src.core.read_conf import get_conf
from src.core.enums.config import LogLevel


class CONF:
    class PATH:
        """路径枚举类"""

        class STR:
            """路径字符串枚举类"""

            TEMP = "临时目录/"
            REPO = "仓库/"
            LOG = "日志/"
            RUN = "运行池/"

        RUN = os.getcwd()
        # 缓存目录
        TEMP = get_conf("path.temp")
        # 仓库目录
        REPO = get_conf("path.repo")
        # 日志地址
        LOG = get_conf("path.app_logs")
        # 运行池
        RUN_JAVA = get_conf("path.Java")
        RUN_STATIC = get_conf("path.Static")
        TRASH = get_conf("path.trash")
        ABS = os.getcwd()
        FM_NGINX_TEMPLATE_CONF = f"{ABS}/conf/template.conf"

    # 本机ip
    def __find_ip():
        __conf_ip = get_conf("local_info.ip")
        if __conf_ip == "0.0.0.0":
            realip = get_realip()
            if os.environ.get('FM_RUNTIME') == 'container':
                return '0.0.0.0'
            else:
                return realip
        else:
            return __conf_ip
    
    # 开发环境
    LOCAL_IP = __find_ip()
    REAL_IP = get_realip()
    # 本机占用端口
    LOCAL_PORT = int(get_conf("local_info.port"))
    # 日志级别
    __loglevel = get_conf("log_level")
    if __loglevel == "DEBUG":
        LOG_LEVEL = LogLevel.DEBUG
    elif __loglevel == "INFO":
        LOG_LEVEL = LogLevel.INFO
    else:
        log.CRITICAL(f"未知日志级别: {__loglevel}")
    # 数据库存放路径
    DATABASE_PATH = get_conf("path.db")


class LAW:
    STR = """~!@#$%^&*()_+`-=~！@#￥%……&*（）——+·-=、】【‘；。，、]\\[';/.,|}{]“：？》《|}{":?><} """
    PORT = [
        0,  # 保留端口
        1,  # tcpmux
        7,  # echo
        9,  # discard
        11,  # systat
        13,  # daytime
        15,  # netstat
        17,  # qotd
        19,  # chargen
        20,  # FTP 数据传输
        21,  # FTP 控制
        22,  # SSH
        23,  # Telnet
        25,  # SMTP
        37,  # 时间协议
        42,  # 主机名服务器
        43,  # WHOIS
        49,  # TACACS
        53,  # DNS
        67,  # DHCP 服务端
        68,  # DHCP 客户端
        69,  # TFTP
        79,  # Finger
        80,  # HTTP
        88,  # Kerberos
        102,  # ISO-TSAP
        110,  # POP3
        111,  # RPCBind
        113,  # ident
        119,  # NNTP
        123,  # NTP
        135,  # 终端服务
        137,  # NetBIOS 名称服务
        138,  # NetBIOS 数据报服务
        139,  # NetBIOS 会话服务
        143,  # IMAP
        161,  # SNMP
        162,  # SNMP Trap
        179,  # BGP
        194,  # IRC
        201,  # AppleTalk
        389,  # LDAP
        443,  # HTTPS
        445,  # Microsoft-DS
        465,  # SMTPS
        514,  # Syslog
        515,  # LPD
        520,  # RIP
        587,  # SMTP (邮件提交)
        631,  # IPP (打印协议)
        636,  # LDAPS
        873,  # rsync
        993,  # IMAPS
        995,  # POP3S
        1080,  # SOCKS
        1194,  # OpenVPN
        1433,  # Microsoft SQL Server
        1434,  # Microsoft SQL Monitor
        1521,  # Oracle 数据库
        1723,  # PPTP
        3306,  # MySQL
        3389,  # 远程桌面
        5432,  # PostgreSQL
        5900,  # VNC
        6379,  # Redis
        8080,  # HTTP 代理
        8443,  # HTTPS 代理
        9000,  # 常见开发端口
        9090,  # 常见开发端口
    ]
    PORT_INTERVAL = ["0-999"]
    ILL_ACC = {"message": "未经许可！"}

    @staticmethod
    def ILL_ACC_sse() -> Iterator[str]:
        ILL_ACC = {"message": "未经许可！"}
        yield f"data: {ILL_ACC}\n\n"


class PATTERN:
    """正则常量"""

    # 用户名规则
    USERNAME = r"^[a-zA-Z0-9_]*$"
    # 密码规则
    PASSWORD = r'^[a-zA-Z0-9_~`!@#$%^&*()\-+=$$$${}|\\;:\'",<.>/?]*$'
    # 邮箱规则
    EMAIL = r"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"
    # 手机号规则
    PHONE = (
        r"^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$"
    )
    # 端口规则
    PORT = (
        r"^(0|[1-9]\d{0,4}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-6])?$"
    )


class HOSTING:
    class BASIC:
        STYPE_LIST = ["Java", "Static"]
        FILE_TYPE = {
            "Java": {"signature": "504B0304", "suffix": ".jar"},
            "Static": {"signature": "504B0304", "suffix": ".zip"},
        }


class DATABASE:
    """数据库配置"""

    DATABASE_LIST: list = [
        "sys_default",
        "hosting_Java",
        "hosting_Static",
        # "middleware",
        # "system",
    ]
    # 数据库存放路径
    DATABASE_PATH = get_conf("path.db")
    # 数据库类型
    DATABASE_TYPE = get_conf("db.type")
    if DATABASE_TYPE == "mysql":
        DATABASE_HOST = get_conf("db.host")
        DATABASE_PORT = get_conf("db.port")
        DATABASE_USER = get_conf("db.user")
        DATABASE_PASSWORD = get_conf("db.passwd")


# 日志块大小，定义一块日志是多少行
BLOCK_SIZE: int = 300


# 文件标签内容
LABEL: str = "fisherman"


# 忽略的分区
GAP_PARTITION = {
    "boot": "/boot",
    "efi": "/boot/efi",
    "tmp": "tmp",
    "run": "/run",
    "dev": "/dev",
    "run*": "/run/user/*",
}


SOFTWARE_PATH = f"{os.getcwd()}/data/software/"
RESOURCE_PATH = f"{os.getcwd()}/resource/"


NGINX_IDENTIFIER = ["运行池", "随机"]


MUST_MERGE_URL = ["/api/apps/hosting/all_apps"]
PROXY_URL = [
    "/api/apps/hosting/realog",
    "/api/apps/hosting/download_file",
    "/api/apps/hosting/receive_file",
]
# 单次读取日志的最大行数
MAX_LINES = 5000
MAX_HISTORY_LINES = 5000